package org.kabeja.dxf.helpers;

import org.kabeja.dxf.entity.DXFMLine;
import org.kabeja.dxf.entity.DXFPolyline;
import org.kabeja.dxf.entity.DXFVertex;
import org.kabeja.dxf.objects.DXFMLineStyle;
import org.kabeja.dxf.objects.DXFMLineStyleElement;
import org.kabeja.math.MathUtils;

/**
 * @author shutao
 * @since 2022/4/25 10:15
 */
public class MLineConverter {
    public MLineConverter() {
    }

    public static DXFPolyline[] toDXFPolyline(DXFMLine var0) {
        DXFMLineStyle var1 = (DXFMLineStyle)var0.getDXFDocument().getDXFObjectByID(var0.getMLineStyleID());
        DXFPolyline[] var2 = new DXFPolyline[var1.getDXFMLineStyleLElementCount()];

        for(int var3 = 0; var3 < var2.length; ++var3) {
            DXFMLineStyleElement var4 = var1.getDXFMLineStyleLElement(var3);
            var2[var3] = new DXFPolyline();
            var2[var3].setDXFDocument(var0.getDXFDocument());
            var2[var3].setLineType(var4.getLineType());
            var2[var3].setColor(var4.getLineColor());
            if (var0.isClosed()) {
                var2[var3].setFlags(1);
            }
        }

        Vector var22 = new Vector();
        new Vector();
        ParametricLine var5 = new ParametricLine();
        ParametricLine var6 = new ParametricLine();

        for(int var7 = 0; var7 < var0.getDXFMLineSegmentCount(); ++var7) {
            DXFMLineSegment var8 = var0.getDXFMLineSegment(var7);
            var22 = var8.getDirection();
            Vector var23 = var8.getMiterDirection();
            var6.setStartPoint(var8.getStartPoint());
            var6.setDirectionVector(var23);

            for(int var9 = 0; var9 < var8.getDXFMLineSegmentElementCount(); ++var9) {
                DXFMLineSegmentElement var10 = var8.getDXFMLineSegmentElement(var9);
                double[] var11 = var10.getLengthParameters();
                Point var12 = var6.getPointAt(var11[0]);
                var5.setStartPoint(var12);
                var5.setDirectionVector(var22);
                var2[var9].addVertex(new DXFVertex(var5.getPointAt(var11[1])));
            }
        }

        if (var1.hasEndRoundCaps()) {
            Point var24 = var2[0].getVertex(var2[0].getVertexCount() - 1).getPoint();
            Point var25 = var2[var2.length - 1].getVertex(var2[var2.length - 1].getVertexCount() - 1).getPoint();
            Vector var26 = MathUtils.getVector(var24, var25);
            double var27 = var26.getLength();
            double var28 = var27 / 2.0D;
            double var14 = Math.sqrt(2.0D) * var28;
            double var16 = var28 - Math.sqrt(0.5D) * var28;
            double var18 = var16 * 2.0D / var14;
            var26.normalize();
            ParametricLine var20 = new ParametricLine(var24, var26);
            Point var21 = var20.getPointAt(var28);
            var20.setStartPoint(var21);
            var22.normalize();
            var20.setDirectionVector(var22);
            var21 = var20.getPointAt(var28);
            var2[0].getVertex(var2[0].getVertexCount() - 1).setBulge(-1.0D * var18);
            var2[0].addVertex(new DXFVertex(var21));
            var2[var2.length - 1].getVertex(var2[var2.length - 1].getVertexCount() - 1).setBulge(var18);
            var2[var2.length - 1].addVertex(new DXFVertex(var21));
        }

        return var2;
    }
}
